//
//  ViewController.m
//  CLGPlayerDemo
//
//  Created by GuochengLiu on 2016/11/28.
//  Copyright © 2016年 sumavision. All rights reserved.
//

#import "CLGViewController.h"
#import "CLGAVPlayerViewController.h"

@import AVKit;

@interface CLGViewController ()

@property (weak, nonatomic) IBOutlet UITextField *urlTextField;

@end

@implementation CLGViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)onHitPlayButton:(UIButton *)sender {
    
    // 检查能否使用原生播放器播放
    
    AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:_urlTextField.text]];
    
    [asset loadValuesAsynchronouslyForKeys:@[@"playable"] completionHandler:^{
        NSError *error = nil;
        AVKeyValueStatus status =
        [asset statusOfValueForKey:@"playable" error:&error];
        switch (status) {
            case AVKeyValueStatusLoaded:
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    CLGAVPlayerViewController *playerVC = [[CLGAVPlayerViewController alloc] initWithMediaURL:[NSURL URLWithString:_urlTextField.text]];
                    [self presentViewController:playerVC animated:YES completion:nil];
                });
            }
                break;
            case AVKeyValueStatusFailed:
            {
                
            }
                break;
            case AVKeyValueStatusCancelled:
                // Loading cancelled
                break;
            default:
                // Handle all other cases
                break;
        }
    }];
}


@end
